package com.xioaye.supermarket.controller;

import com.xioaye.supermarket.entity.User;
import com.xioaye.supermarket.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/login")
    public String login(String username, String password, Model model) {
        User user = userMapper.queryUser(username, password);
        if (user == null) {
            model.addAttribute("msg", "用户名或密码不存在！");
            return "index";
        } else {
            model.addAttribute("msg", user.getUsername());
            return "shop";
        }
    }

    @RequestMapping("/register")
    public String register(String username, String password, Model model) {
        int i = userMapper.addUser(username, password);
        if (i < 1) {
            model.addAttribute("msg", "注册失败");
            return "register";
        }
        return "index";
    }

    @RequestMapping("/registerView")
    public String registerView() {
        return "register";
    }
}
